const api = require("../../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workOrderInfo: {},
    hasEvaluateSource1: true // 是否有 evaluateSource === 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取传递参数
    const workOrderCode = options.workOrderCode;
    console.log('传递workOrderCode:', workOrderCode);

    this.getWorkOrderDetail(workOrderCode);
  },
  getWorkOrderDetail: function (workOrderCode) {
    wx.request({
      url: api.workOrderInfo,
      method: 'POST',
      data: {
        workOrderCode,
      },
      success: (res) => {
        wx.hideLoading()
        if (res.data.code == 200) {
          this.setData({
            workOrderInfo: res.data.data
          }, () => {
            this.splitImageUrlsBy();
            console.log('工单详情返回', res.data.data)
            this.setEvaBtn();
          });
        } else {
          wx.showToast({
            title: res.data.mesage,
            icon: "none"
          })
        }
      }
    })
  },
  setEvaBtn() {
    const {
      evaluateResps
    } = this.data.workOrderInfo;
    // 判断是否有 evaluateSource === 1
    const hasEvaluateSource1 = !evaluateResps.some(item => item.evaluateSource === 1)
    console.log('评价按钮：', hasEvaluateSource1)
    this.setData({
      hasEvaluateSource1
    });
  },
  // 分割图片
  splitImageUrls() {
    const {
      workOrderImgFile
    } = this.data.workOrderInfo;
    if (workOrderImgFile) {
      // 根据 https 分割，并添加 https 前缀到每个图片
      const photoList = workOrderImgFile
        .split("https")
        .filter((url) => url) // 过滤掉空字符串
        .map((url) => "https" + url); // 拼接 https 前缀
      this.setData({
        "workOrderInfo.photoList": photoList
      });
      console.log("分割后的图片列表:", photoList);
    } else {
      console.log("没有图片 URL 可分割");
    }
  },
  splitImageUrlsBy() {
    const {
      workOrderImgFile
    } = this.data.workOrderInfo;
    if (workOrderImgFile) {
      var pic = [];
      if (workOrderImgFile.includes('&&')) {
        // 使用逗号切割字符串
        pic = workOrderImgFile.split('&&');
      } else {
        // 如果没有逗号，则直接将 workOrderImgFile 放入 pic 数组中
        pic = [workOrderImgFile];
      }
      console.log('切割之后的数组', pic);
      this.setData({
        // 把数组内容直接赋值给testingImgFile
        'workOrderInfo.photoList': pic
      });
    }
  },
  onCheckResult() {
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyWorkOrder/Result/Result?workOrderCode=${this.data.workOrderInfo.workOrderCode}&resultType=1&testingCode=${this.data.workOrderInfo.testingCode}`
      // ${this.data.workOrderInfo.testingCode}
      // testingCode=TEST20241112095532
    });
  },
  onFixResult() {
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyWorkOrder/Result/Result?workOrderCode=${this.data.workOrderInfo.workOrderCode}&resultType=2&repairCode=${this.data.workOrderInfo.repairCode}`
      // repairCode=REPAIR20241112141825
    });
  },
  onEvaluate() {
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyPage/MyEstimation/MyEstimation?workOrderCode=${this.data.workOrderInfo.workOrderCode}&feedbackCode=${this.data.workOrderInfo.feedbackCode}`
    });
  },
  onBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})